package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.Appinfo;
import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.mapper.AppmanagerMapper;
import com.ruoyi.system.service.IAppmanagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BuildinfoMapper;
import com.ruoyi.system.domain.Buildinfo;
import com.ruoyi.system.service.IBuildinfoService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 楼栋信息Service业务层处理
 * 
 * @author yaozh
 * @date 2024-03-04
 */
@Service
public class BuildinfoServiceImpl implements IBuildinfoService 
{
    @Autowired
    private BuildinfoMapper buildinfoMapper;

    @Resource
    private IAppmanagerService appManagerService;
    @Autowired
    private AppmanagerMapper appmanagerMapper;

    /**
     * 查询楼栋信息
     * 
     * @param buildid 楼栋信息主键
     * @return 楼栋信息
     */
    @Override
    public Buildinfo selectBuildinfoByBuildid(Long buildid,String dbName)
    {
        return buildinfoMapper.selectBuildinfoByBuildid(buildid,dbName);
    }

    /**
     * 查询楼栋信息列表
     * 
     * @param buildinfo 楼栋信息
     * @return 楼栋信息
     */
    @Override
    public List<Buildinfo> selectBuildinfoList(Long[] appIds,Buildinfo buildinfo,String dbName)
    {
        return buildinfoMapper.selectBuildinfoList(appIds,buildinfo,dbName);
    }

    /**
     * 新增楼栋信息
     * 
     * @param buildinfo 楼栋信息
     * @return 结果
     */
    @Override
    public int insertBuildinfo(Buildinfo buildinfo,String dbName)
    {
        Appmanager info = appManagerService.selectAppmanagerByAppid(buildinfo.getAppid());
        if(info != null){
            buildinfo.setAppid(info.getAppid());
            buildinfo.setAppname(info.getAppName());
        }else{
            return -2;
        }
        return buildinfoMapper.insertBuildinfo(buildinfo,dbName);
    }

    /**
     * 修改楼栋信息
     * 
     * @param buildinfo 楼栋信息
     * @return 结果
     */
    @Override
    public int updateBuildinfo(Buildinfo buildinfo,String dbName)
    {
        return buildinfoMapper.updateBuildinfo(buildinfo,dbName);
    }

    /**
     * 批量删除楼栋信息
     * 
     * @param buildids 需要删除的楼栋信息主键
     * @return 结果
     */
    @Override
    public int deleteBuildinfoByBuildids(Long[] buildids,String dbName)
    {
        return buildinfoMapper.deleteBuildinfoByBuildids(buildids,dbName);
    }

    /**
     * 删除楼栋信息信息
     * 
     * @param buildid 楼栋信息主键
     * @return 结果
     */
    @Override
    public int deleteBuildinfoByBuildid(Long buildid,String dbName)
    {
        return buildinfoMapper.deleteBuildinfoByBuildid(buildid,dbName);
    }

    /**
     * 返回用户权限范围内的数据，且项目数据库参数等于dbName
     * @param loginUser
     * @param dbName
     * @return
     */
    @Override
    public Long[] listAppIds(LoginUser loginUser, String dbName){
        List<Appmanager> list = getAppManagerList(loginUser,dbName);
        List<Long> appIdList = new ArrayList<>();
        for(Appmanager appmanager : list){
            appIdList.add(appmanager.getAppid());
        }
        return appIdList.toArray(new Long[0]);
    }

    /**
     * 返回用户权限范围内的数据，且项目数据库参数等于dbName
     * @param loginUser
     * @param dbName
     * @return
     */
    @Override
    public List<Appmanager> getAppManagerList(LoginUser loginUser,String dbName){
        return appManagerService.selectAppManagerListByUserAndDbName(loginUser,dbName);
    }
}
